<?php

class ConfigParent extends Engine {
	protected static $CONFIG;
	protected static $FINAL_CONFIG;

	protected static $DEFAULT_CONFIG = array(
		'DEBUG' => true,
		'DB_HOST' => 'localhost',
		'DB_USER' => 'engine',
		'DB_PASSWORD' => 'J8fHgfYtGew3gd73vGd',
		'DB_NAME' => 'engine',
		'LOGS_EXT' => '.log',
		'LOGS_SEP' => "\n==============\n",
		'LOGS_DEFAULT_NAME' => 'general',
		'DATE_TIME_FORMAT_USER' => 'd.m.Y H:i:s',
		'DATE_FORMAT_USER' => 'd.m.Y',
		'DATE_TIME_FORMAT_DB' => 'Y-m-d H:i:s',
		'DATE_FORMAT_DB' => 'Y-m-d',
		'RESULTS_PER_PAGE' => 30,
		'DEFAULT_TIMEZONE' => 'Europe/Berlin',
		'EXCHANGE_RATES_API_URL' => 'http://openexchangerates.org/api/latest.json?app_id=b4abdd70c08b48908b31752ec6d57abe',
		'PUBLIC_HOLIDAY_URL' => 'http://www.feiertage.net/csvfile.php?state=BY&year=#year#&type=csv'
	);

	/**
	 * Add here the configuration which are not raw strings/integers/boolean.
	 * For example if you would add this: self::$DEFAULT_CONFIG['ERROR_LOGS_PATH'] = DS.'trash'.DS.'logs'.DS;
	 * as default value when declaring the $DEFAULT_CONFIG it would give parse error.
	 *
	 * @static
	 *
	 */
	protected static function loadExtraConfig() {
		self::$DEFAULT_CONFIG['BASE_DOMAIN'] = null;
		self::$DEFAULT_CONFIG['TRASH_PATH'] = DS.'trash'.DS;
		self::$DEFAULT_CONFIG['LOGS_PATH'] = self::$DEFAULT_CONFIG['TRASH_PATH'].'logs'.DS;
		self::$DEFAULT_CONFIG['ERROR_LOGS_PATH'] = self::$DEFAULT_CONFIG['TRASH_PATH'].'errors'.DS;
		// TODO: Define useful data path
		self::$DEFAULT_CONFIG['MAIL_PATH'] = self::$DEFAULT_CONFIG['TRASH_PATH'].'mail'.DS;
		self::$DEFAULT_CONFIG['SESSION_PATH'] = self::$DEFAULT_CONFIG['TRASH_PATH'].'sessions'.DS;
		self::$DEFAULT_CONFIG['SESSION_NAME'] = 'ng';
		self::$DEFAULT_CONFIG['SESSION_COOKIE_LIFETIME'] = 3600 * 24 * 365;
		self::$DEFAULT_CONFIG['SESSION_COOKIE_URL'] = self::$DEFAULT_CONFIG['BASE_DOMAIN'];
		self::$DEFAULT_CONFIG['SESSION_LOGIN_SHORT_LIFETIME'] = 3600;
		self::$DEFAULT_CONFIG['SESSION_LOGIN_LONG_LIFETIME'] = self::$DEFAULT_CONFIG['SESSION_COOKIE_LIFETIME'];
		self::$DEFAULT_CONFIG['USE_MEMORY_LIST'] = true;
		self::$DEFAULT_CONFIG['TEMPORARY_FILE_PUBLIC_HOLIDAYS'] = ROOT_PATH . 'public/static/common/cron/public_holiday.tmp';
		//allowed extensions and file types
		self::$DEFAULT_CONFIG['ALLOWED_EXTENSIONS'] = array(
			'users' => array(
				'jpg',
				'jpeg',
				'png',
				'gif'
			),
			'default' => array(
				'jpg',
				'jpeg',
				'png',
				'swf',
				'pdf'
			),
			'invoice' => array(
				'pdf'
			)
		);

		//items per page for page pagination
		self::$DEFAULT_CONFIG['DEFAULT_ITEMS_PER_PAGE'] = 12;
	}

	/**
	 * Add here the configuration which require a connection to the database.
	 * This function is called after the MySQL class was loaded.
	 *
	 * @static
	 *
	 */
	public static function loadConfigFromDb() {
		/**
		 * Configuring the languages
		 */
		self::$FINAL_CONFIG['LANGS_FULL'] = Db_Locale_Languages::getLangs(); // Array with all languages parameters
		if (self::$S instanceof Session) {
			self::$FINAL_CONFIG['LANG'] = self::$S->Get('lang'); // Current selected language definition. Ex. 'en', 'ru', etc.
		}
		foreach(self::$FINAL_CONFIG['LANGS_FULL'] as $lang) {
			self::$FINAL_CONFIG['LANGS'][$lang['id']] = $lang['l_define']; // Array with all languages definition
			self::$FINAL_CONFIG['LANGS_ID'][$lang['l_define']] = $lang['id']; // Array with all languages id
			if (!empty(self::$FINAL_CONFIG['LANG']) && self::$FINAL_CONFIG['LANG'] == $lang['l_define']) {
				self::$FINAL_CONFIG['LANG_ID'] = $lang['id']; // Current selected language ID
			} else {
				if ($lang['is_default'] == 1 && empty(self::$FINAL_CONFIG['LANG'])) {
					self::$FINAL_CONFIG['LANG'] = $lang['l_define']; // Current selected language definition. Ex. 'en', 'ru', etc.
					self::$FINAL_CONFIG['LANG_ID'] = $lang['id']; // Current selected language ID
				}
			}
			if ($lang['is_default'] == 1) {
				self::$FINAL_CONFIG['DEFAULT_LANG'] = $lang['l_define']; // The default language definition
				self::$FINAL_CONFIG['DEFAULT_LANG_ID'] = $lang['id']; // The default language ID
			}
			if ($lang['l_country_based'] == 1) {
				self::$FINAL_CONFIG['COUNTRY_LANG'] = $lang['l_define']; // The country based language definition
				self::$FINAL_CONFIG['COUNTRY_LANG_ID'] = $lang['id']; // The country based language ID
			}
		}
		if (self::$S instanceof Session) {
			self::$S->Set('lang', self::$FINAL_CONFIG['LANG']);
		}

		// Loading the translations for the current project's language (user's session or project's default language)
		Db_Locale_Translations::loadTrans(self::$FINAL_CONFIG['LANG_ID']);

		if (PROJECT == 'admin') {
			// For every admin panel from any country we load additionally the translations for country base language.
			Db_Locale_Translations::loadTrans(self::$FINAL_CONFIG['COUNTRY_LANG_ID'], true);
		}

		self::$FINAL_CONFIG['LANGS_DETAILS'] = array();
		foreach(self::$FINAL_CONFIG['LANGS_FULL'] as $lang) {
			self::$FINAL_CONFIG['LANGS_DETAILS'][$lang['l_define']] = array(
				'name' => t('language', $lang['l_name_key']),
				'short_name' => t('language', $lang['l_short_name_key'])
			);
		}

		// Setting the ENVIRONMENT in dependency of the current URL
		$currentUrl = Utils::getCurrentUrl();
		if ($currentUrl) {
			if (strpos($currentUrl, self::$PROJECT['pr_domain_test']) !== false) {
				self::$FINAL_CONFIG['ENVIRONMENT'] = 'test';
			} elseif(strpos($currentUrl, self::$PROJECT['pr_domain_live']) !== false) {
				self::$FINAL_CONFIG['ENVIRONMENT'] = 'live';
			} else {
				self::$FINAL_CONFIG['ENVIRONMENT'] = 'test';
			}
		} else {
			//request is from flash
			self::$FINAL_CONFIG['ENVIRONMENT'] = 'test';
		}

	}

	/**
	 * Used for getting the configuration variables. As a shortcut you should use
	 * the simple @function conf('PARAM') which is calling this function.
	 *
	 * @static
	 * @param $key
	 * @return mixed
	 */
	public static function get($key) {
		return self::$FINAL_CONFIG[$key];
	}

	/**
	 * Used for modifying the configuration variables after the initialization of config
	 * @param string $key The name of the config variable to set
	 * @param mixed $value The value of the variable
	 */
	public static function set($key, $value) {
		self::$FINAL_CONFIG[$key] = $value;
	}

	/**
	 * Preparing the configuration arrays. This method is called only once in the initialize.php file.
	 * There should be no reason to call this method again anywhere in the code.
	 *
	 * @static
	 *
	 */
	public static function init() {
		static::loadExtraConfig();
		self::$FINAL_CONFIG = array_merge(self::$DEFAULT_CONFIG, static::$CONFIG);
	}
}
